<template>
  <div>
    <el-card class="box-card">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-row class="addUser">
        <el-button type="primary">添加角色</el-button>
      </el-row>

      <el-table :data="roleList"
                border
                stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['borderBottom', idex1 === 0 ? 'borderTop' : '', 'center']"
                    v-for="(item1, idex1) in scope.row.children"
                    closable
                    @close="removeRight(scope.row, item1.id)"
                    :key="item1.id">
              <el-col :span="5">
                <el-tag class="info1">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row :class="[idx2 === 0 ? 'borderTop' : '', 'center']"
                        v-for="(item2, idx2) in item1.children"
                        :key="item2.id"
                        closable
                        @close="removeRight(scope.row, item2.id)">
                  <el-col :span="6">
                    <el-tag type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag type="warning"
                            v-for="(item3, idx3) in item2.children"
                            closable
                            @close="removeRight(scope.row, item3.id)"
                            :key="item3.id">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="roleName"
                         label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc"
                         label="角色描述">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini">编辑</el-button>
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini">删除</el-button>
            <el-button type="warning"
                       icon="el-icon-setting"
                       @click="setRightDialog(scope.row)"
                       size="mini">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="分配用户权限"
                 :visible.sync="showDialog"
                 @close="setRight"
                 width="30%">
        <el-tree :data="rightslist"
                 show-checkbox
                 node-key="id"
                 default-expand-all
                 :default-checked-keys="defKeys"
                 ref="treeRef"
                 :props="treeProps">
        </el-tree>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary"
                     @click="setRightRoles">确 定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      roleList: [],
      showDialog: false,
      rightslist: [],
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      defKeys: [],
      roleId: ''
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败！')
      }
      this.roleList = res.data
    },
    async removeRight (user, id) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      const { data: res } = await this.$http.delete(`roles/${user.id}/rights/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除权限失败')
      }
      this.$message.success('删除用户成功！')
      user.children = res.data
    },
    async setRightDialog (role) {
      this.roleId = role.id
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限数据失败')
      }
      this.rightslist = res.data
      this.getDefKeys(role, this.defKeys)
      this.showDialog = true
    },
    getDefKeys (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getDefKeys(item, arr))
    },
    setRight () {
      this.defKeys = []
    },
    async setRightRoles () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idStr = keys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: idStr })
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败！')
      }
      this.$message.success('分配权限成功！')
      this.getRoleList()
      this.showDialog = false
    }
  }
}
</script>

<style lang="less" scoped>
.addUser {
  margin: 15px 0;
}
.el-tag {
  margin: 5px 5px;
}
.borderTop {
  border-top: 1px solid #eee;
}
.borderBottom {
  border-bottom: 1px solid #eee;
}
.center {
  display: flex;
  align-items: center;
}
</style>
